package com.ojoin.trade.common.utils.excel;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.io.IOUtils;

/**
 * CSV
 */
public class CSVReader {

	private IRowReader rowReader;

	private String encodeing="UTF-8";
	
	public String getEncodeing() {
		return encodeing;
	}

	public void setEncodeing(String encodeing) {
		this.encodeing = encodeing;
	}

	public void setRowReader(IRowReader rowReader) {
		this.rowReader = rowReader;
	}

	/**
	 * @throws IOException
	 */
	public void process(InputStream ins) throws IOException {

		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(ins,encodeing));
			String line = "";
			int curRow = 0;
			while ((line = br.readLine()) != null) {
				// 存储行记录的容器
				List rowlist = new ArrayList();
				String[] items=line.split(",");
				for (String item:items) {
					rowlist.add(item);
				}
				rowReader.getRows(0, "", curRow, rowlist);
				curRow++;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(ins);
		}
	}

}
